import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import Main from '../views/Main.vue'
import Create from '../views/Create.vue'
import CreateList from '../views/CreateList.vue'
import ItemCreate from '../views/ItemCreate.vue'
import ItemList from '../views/ItemList.vue'
import HeroesCreate from '../views/HeroesCreate'
import HeroesList from '../views/HeroesList'
import ArticlesCreate from '../views/ArticlesCreate'
import ArticlesList from '../views/ArticlesList'
import AdsCreate from '../views/AdsCreate'
import AdsList from '../views/AdsList'
import AdminCreate from '../views/AdminCreate'
import AdminList from '../views/AdminList'
import Login from '../views/Login'
import JournalismeCreate from '../views/JournalismeCreate'
import JournalismeList from '../views/JournalismeList'
import VideoCreate from '../views/VideoCreate'
import VideoList from '../views/VideoList'
Vue.use(VueRouter)

const routes = [
  {path:'/login',name:'login',component:Login,meta:{ isPublic:true }},
  {
    path: '/',
    name: 'Main',
    component: Main,
    children:[
      {path:'/categories/create',component:Create},
      {path:'/categories/list',component:CreateList},
      {path:'/items/create',component:ItemCreate},
      {path:'/items/list',component:ItemList},
      {path: '/heroes/create',component:HeroesCreate},
      {path:'/heroes/list',component:HeroesList},
      {path:'/articles/create',component:ArticlesCreate},
      {path:'/articles/list',component:ArticlesList},
      {path:'/ads/create',component:AdsCreate},
      {path:'/ads/list',component:AdsList},
      {path:'/admin_users/create',component:AdminCreate},
      {path:'/admin_users/list',component:AdminList},
      {path:'/journalismes/create',component:JournalismeCreate},
      {path:'/journalismes/list',component:JournalismeList},
      {path:'/videolist/create',component:VideoCreate},
      {path:'/videolist/list',component:VideoList}
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from ,next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login')
  }
  next()
})
export default router
